package com.fsc.fragment;

import java.util.List;

import com.fsc.fragment.flag.Flag;
import com.fsc.fragment.flag.FlagDao;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FlagActivity extends Fragment implements OnClickListener {
	private TextView btn_flag;
	private ListView lv_flag;
	private flagAdapter adapter;
	private FlagDao Dao;
	private List<Flag> data;
	private int position;
	private View v;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.activity_flag,container, false);
		btn_flag = (TextView) v.findViewById(R.id.btn_flag);
		lv_flag = (ListView) v.findViewById(R.id.lv_flag);
		
		adapter = new flagAdapter();
		Dao = new FlagDao(getActivity());
		
		data = Dao.queryFlag();
		adapter.notifyDataSetChanged();
		lv_flag.setAdapter(adapter);
		lv_flag.setOnCreateContextMenuListener(this);
		btn_flag.setOnClickListener(this);
		return v;
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		data.clear();
		data = Dao.queryFlag();
		adapter.notifyDataSetChanged();
		lv_flag.setAdapter(adapter);
	}
	//创建适配器
	class flagAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return data.size();
		}
		@Override
		public Object getItem(int position) {
			return data.get(position);
		}
		@Override
		public long getItemId(int position) {
			return 0;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHodler hodler=null;
			if(convertView == null){//判断缓存对象是否为null,不为空时已经缓存了对象
				hodler=new ViewHodler();
				convertView=View.inflate(getActivity(),R.layout.item_flag, null);
				hodler.tv_flag = (TextView)convertView.findViewById(R.id.tv_flag);
				hodler.tv_time=(TextView)convertView.findViewById(R.id.tv_time);
				convertView.setTag(hodler);
				
			}else{//等于null说明第一次显示，则TextView都是新创建的
				hodler=(ViewHodler)convertView.getTag();
			}
			Flag flag = data.get(position);

			
			hodler.tv_flag.setText(flag.getFlag());
			hodler.tv_time.setText(flag.getTime());
			return convertView;
		}	
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("请选择操作！");
		menu.setHeaderIcon(R.drawable.ic_launcher);
		menu.add(0, 1, 0, "编辑");
		menu.add(0, 2, 0, "删除");
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
		position = info.position;
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		final Flag flag = data.get(position);
		switch (item.getItemId()) {
		case 1://1.编辑
			int id = flag.getId();
			Intent intent = new Intent(getActivity(),AddFlagActivity.class);
			intent.putExtra("id", id);
			intent.putExtra("time", flag.getTime());
			intent.putExtra("flag", flag.getFlag());
			intent.putExtra("type",1);
			startActivityForResult(intent, 1);
			break;

		default://2.删除
			new AlertDialog.Builder(getActivity())
			.setTitle("是否确定删除？")
			.setPositiveButton("确定",new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					//1.删除数据表对应的数据
					Dao.deleteFlag(flag.getId());
						//2.删除List对应的数据
					data.remove(position);
						//3.通知更新列表
					adapter.notifyDataSetChanged();
				}
			})
			//3.取消按钮
			.setNegativeButton("取消", null)
			.show();
			break;
		}
		return super.onContextItemSelected(item);
	};
	@Override
	public void onClick(View v) {
		if(v == btn_flag){
			Intent Intent = new Intent(getActivity(),AddFlagActivity.class);
			Intent.putExtra("type",3);
			startActivityForResult(Intent,3);
		}
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if(requestCode == 3 && resultCode == 3){
			Flag flag = new Flag();
			//1、设置数据
			flag.setFlag(intent.getStringExtra("flag"));
			flag.setTime(intent.getStringExtra("time"));
			
			//2、执行添加操作
			Dao.insertFlag(flag);
			//3、添加到list中
			data.add(0,flag);
			//4、通知list更新
			adapter.notifyDataSetChanged();
			Toast.makeText(getActivity(), "新增便签成功！", Toast.LENGTH_LONG).show();
		}
		super.onActivityResult(requestCode, resultCode, intent);
	} 
}